package com.echo.echovividlive.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.echo.echovividlive.common.UserThread;
import com.echo.echovividlive.domain.dto.CommentQueryDTO;
import com.echo.echovividlive.domain.entity.Comment;
import com.echo.echovividlive.domain.entity.Result;
import com.echo.echovividlive.domain.entity.User;
import com.echo.echovividlive.domain.entity.Video;
import com.echo.echovividlive.domain.vo.CommentQueryVO;
import com.echo.echovividlive.domain.vo.PageVO;
import com.echo.echovividlive.mapper.CommentMapper;
import com.echo.echovividlive.mapper.UserMapper;
import com.echo.echovividlive.mapper.VideoMapper;
import com.echo.echovividlive.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.echovividlive.utils.TimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiajunjie
 * @since 2024-10-22
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final CommentMapper commentMapper;
    private final VideoMapper videoMapper;
    private final UserMapper userMapper;
    @Override
    @Transactional
    public Result comment(Comment comment) {
        comment.setCreateTime(TimeUtil.getCurrentTime());
        comment.setSenderId(UserThread.getUserId());

        LambdaUpdateWrapper<Video> videoLambdaQueryWrapper = new LambdaUpdateWrapper<Video>()
                .eq(Video::getVideoId,comment.getVideoId())
                .setSql("comment_count = comment_count + 1");
        videoMapper.update(videoLambdaQueryWrapper);
        this.save(comment);
        CommentQueryVO commentQueryVO = BeanUtil.copyProperties(comment, CommentQueryVO.class);
        User user = userMapper.selectById(UserThread.getUserId());
        commentQueryVO.setAvatar(user.getAvatar());
        commentQueryVO.setNickName(user.getNickName());
        return Result.success(commentQueryVO);
    }

    @Override
    public Result listComment(CommentQueryDTO commentQueryDTO) {
        // 查看一级评论
        PageHelper.startPage(commentQueryDTO.getPageIndex()==null?1:commentQueryDTO.getPageIndex()
                ,commentQueryDTO.getPageSize()==null?5:commentQueryDTO.getPageSize());
        if (commentQueryDTO.getCalledId() == null) {
            Page<CommentQueryVO> commentQueryVOS = commentMapper.pageList(commentQueryDTO);
            return Result.success(new PageVO(commentQueryVOS.getTotal(),commentQueryVOS.getResult()));
        }

        // 查看二级评论
        Page<CommentQueryVO> commentQueryVOS = commentMapper.pageListSecond(commentQueryDTO);
        return Result.success(new PageVO(commentQueryVOS.getTotal(),commentQueryVOS.getResult()));
    }

    @Override
    @Transactional
    public Result deleteCommentById(Integer id) {
        Comment comment = commentMapper.selectById(id);
        Video video = videoMapper.selectById(comment.getVideoId());
        if (!Objects.equals(video.getUserId(), UserThread.getUserId()) && !Objects.equals(comment.getSenderId(), UserThread.getUserId())) {
            return Result.error("无法删除他人评论");
        }
        int i = commentMapper.deleteById(id);
        // 删除二级评论
        int delete = commentMapper.delete(new LambdaUpdateWrapper<Comment>().eq(Comment::getCalledId, id));
        video.setCommentCount(video.getCommentCount()-i-delete);
        videoMapper.updateById(video);
        return Result.success();
    }
}
